package com.zanfeng.signleplatform.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zanfeng.signleplatform.common.baidu.TransApi;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class TransUtil {

    private final static String appId = "20160901000027969";
    private final static String securityKey = "1Sxr6d3K3PlZZuECIaeE";

    public static final char UNDERLINE = ' ';

    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static void process(List<String> lineList, JSONObject jsonObject, String parent, String key) {
        String prefix = StringUtils.isNotBlank(parent) ? parent + "." : "";
        if (jsonObject.get(key) instanceof JSONArray) {
            JSONArray tempArray = (JSONArray) jsonObject.get(key);
            if (tempArray != null && tempArray.size() > 0) {
                if (tempArray.get(0) instanceof JSONObject) {
                    JSONObject tempJson = (JSONObject) tempArray.get(0);
                    for (String tempKey : tempJson.keySet()) {
                        process(lineList, tempJson, prefix + key, tempKey);
                    }
                } else {
                    lineList.add(prefix + key);
                }
            } else {
                lineList.add(prefix + key);
            }
        } else if (jsonObject.get(key) instanceof JSONObject) {
            JSONObject tempJson = (JSONObject) jsonObject.get(key);
            for (String tempKey : tempJson.keySet()) {
                process(lineList, tempJson, prefix + key, tempKey);
            }
        } else {
            lineList.add(prefix + key);
        }
    }

    /**
     * 转换内容
     *
     * @param jsonObject
     * @return
     */
    public static String parseAndTrans(JSONObject jsonObject) {
        List<String> lineList = new ArrayList<>();
        Set<String> keySet = jsonObject.keySet();
        for (String key : keySet) {
            process(lineList, jsonObject, "", key);
        }
        LinkedHashMap<String, String> lineMap = new LinkedHashMap<>();
        List<String> transList = new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        for (String line : lineList) {
            String transText = line;
            if (StringUtils.indexOf(line, ".") != -1) {
                transText = StringUtils.substringAfterLast(line, ".");
            }
            transText = camelToUnderline(transText);
            lineMap.put(line, transText);
            transList.add(transText);
            stringBuffer.append(transText).append("\r\n");
        }
        TransApi api = new TransApi(appId, securityKey);
        String result = api.getTransResult(stringBuffer.toString(), "en", "zh");
        JSONArray transResultArray = new JSONArray();
        JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson.getJSONArray("trans_result").size() > 0) {
            transResultArray.addAll(resultJson.getJSONArray("trans_result"));
        }
        Map<String, String> tranMap = new HashMap<>();
        if (transResultArray != null) {
            for (Object tranObj : transResultArray) {
                JSONObject tranJson = (JSONObject) tranObj;
                String src = tranJson.getString("src");
                String dst = tranJson.getString("dst");
                tranMap.put(src, dst);
            }
        }
        HashMap<String, String> wordMap = new HashMap<>();
        StringBuffer resultStringBuffer = new StringBuffer();
        for (String key : lineMap.keySet()) {
            String cnWord = ObjectUtils.firstNonNull(tranMap.get(lineMap.get(key)), key);
            String keyWord = StringUtils.substringAfterLast(key, ".");
            wordMap.put(keyWord, cnWord);
        }
        for (Map.Entry<String, String> entry : wordMap.entrySet()) {
            resultStringBuffer.append(entry.getKey() + ":" + entry.getValue()).append("\r\n");
        }
        return resultStringBuffer.toString();
    }

}
